package api

import (
	__ "api-gateway/basic/proto"
	"api-gateway/handler/consts"
	"api-gateway/handler/request"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	jwt2 "github.com/golangblogs/gojwt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
	"time"
)

func Register(c *gin.Context) {
	var req request.CreateQuestionReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	question, err := c1.Register(c, &__.RegisterRequest{
		Mobile: req.ExName,
	})
	j := jwt2.NewJWT(consts.JWT_SECRET)
	claims := jwt2.CustomClaims{
		ID: uint(question.Id),

		StandardClaims: jwt.StandardClaims{ExpiresAt: time.Now().UnixMilli() * 3600},
	}
	token, err := j.CreateToken(claims)
	if err != nil {
		return
	}
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data":  question,
		"token": token,
	})
}

func AddExam(c *gin.Context) {
	var req request.AddExamReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	question, err := c1.AddExam(c, &__.AddExamRequest{
		StudentId:      int64(c.GetUint("studentId")),
		TestPaperId:    int64(req.TestPaperId),
		TestQuestionId: int64(req.TestQuestionId),
		ExamStatus:     int64(req.ExamStatus),
		Score:          req.Score,
		RightWrong:     int64(req.RightWrong),
		StartTime:      int64(req.StartTime),
		EndTime:        int64(req.EndTime),
		ParticipatIn:   int64(req.ParticipatIn),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": question,
	})
}

func InfoExam(c *gin.Context) {
	var req request.InfoExamReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	question, err := c1.InfoExam(c, &__.InfoExamRequest{
		StudentId: int64(req.StudentId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": question,
	})
}

func AnswerExam(c *gin.Context) {
	var req request.AnswerExamReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	question, err := c1.AnswerExam(c, &__.AnswerExamRequest{
		StudentId:   int64(c.GetUint("studentId")),
		TestPaperId: int64(req.TestPaperId),
		RightWrong:  int64(req.RightWrong),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": question,
	})
}

func MarkingExam(c *gin.Context) {
	var req request.MarkingExamReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	question, err := c1.MarkingExam(c, &__.MarkingExamRequest{
		StudentId:   int64(c.GetUint("studentId")),
		TestPaperId: int64(req.TestPaperId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": question,
	})
}

func Transcript(c *gin.Context) {
	var req request.TranscriptReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	question, err := c1.Transcript(c, &__.TranscriptRequest{
		StudentId:   int64(c.GetUint("studentId")),
		TestPaperId: int64(req.TestPaperId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": question,
	})
}
